package com.example.click.huanxin.ui;

import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.click.huanxin.presenter.callback.RegisterListener;
import com.example.click.huanxin.presenter.impl.RegisterImpl;
import com.example.click.huanxin.utils.CheckInfo;
import com.example.click.mvpfoot.R;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class RegisterLoginActivity extends BaseActivity implements TextView.OnEditorActionListener{

    @InjectView(R.id.register_et_username)
    EditText registerEtUsername;
    @InjectView(R.id.register_til_username)
    TextInputLayout registerTilUsername;
    @InjectView(R.id.register_et_password)
    EditText registerEtPassword;
    @InjectView(R.id.register_til_password)
    TextInputLayout registerTilPassword;
    @InjectView(R.id.button)
    Button button;
        public RegisterImpl register=new RegisterImpl();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_login);
        ButterKnife.inject(this);
        registerEtPassword.setOnEditorActionListener(this);
    }

    @OnClick(R.id.button)
    public void onClick() {
        regist();
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
       if (v.getId()== R.id.register_et_password){
                if (actionId== EditorInfo.IME_ACTION_DONE){
                    regist();
                    return true;
                }
        }
        return false;
    }

    private void regist() {
        String user = registerEtUsername.getText().toString().trim();
        String pw = registerEtPassword.getText().toString().trim();
        if (!CheckInfo.checkUsername(user)){
            registerTilUsername.setErrorEnabled(true);
            registerTilUsername.setError("用户名不符合规则");
            return;
        }
        register.regist(user, pw, new RegisterListener() {
            @Override
            public void onSuccess() {
                startActivity(RegisterActivity.class);
            }

            @Override
            public void onError(ArrayList arrayList) {
                if (arrayList.size()>0){

                }
            }
        });
    }

    public void showSuccess() {

    }
}
